using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BENBUN.ExportCRS
{
    public partial class crsExportForm : Form
    {
        public crsExportForm()
        {
            InitializeComponent();  
        }

        private void crsExportForm_Load(object sender, EventArgs e)
        {
            textfilename.Text = BenbunPlugin.Instance.exportFileName;
            numSpeed.Value = (decimal)BenbunPlugin.Instance.newSpeedValue;

            if (BenbunPlugin.Instance.originalTime)
            {
                rb1.Checked = true;
                rb2.Checked = false;
            }
            else
            {
                rb1.Checked = false;
                rb2.Checked = true;
            }

            switch (BenbunPlugin.Instance.newSpeedUnits)
            {

                case "minutespermile":
                    comboSpeedUnits.SelectedIndex = 2;
                    break;
                case "minutesperkm":
                    comboSpeedUnits.SelectedIndex = 3;
                    break;
                case "kmperhour":
                    comboSpeedUnits.SelectedIndex = 1;
                    break;
                case "milesperhour":
                    comboSpeedUnits.SelectedIndex = 0;
                    break;
                default:
                    Console.WriteLine("Default case");
                    break;
            }
            
        }
        private void button2_Click(object sender, EventArgs e)
        {
            doExport = DialogResult.Yes;
            this.Close();
        }

        public DialogResult doExport;

        private void button3_Click(object sender, EventArgs e)
        {
            doExport = DialogResult.No;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileDialog dialog = new SaveFileDialog();
            dialog.Filter = "CRS Route File (*.crs)|*.crs|All files|*.*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    textfilename.Text = dialog.FileName;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void textfilename_TextChanged(object sender, EventArgs e)
        {
            BenbunPlugin.Instance.exportFileName = textfilename.Text;
            if (textfilename.Text.Trim().Length == 0)
            { button2.Enabled = false; }
            else 
            { button2.Enabled = true; }
        }

        private void numSpeed_ValueChanged(object sender, EventArgs e)
        {
            BenbunPlugin.Instance.export_speed = numSpeed.Value;
            BenbunPlugin.Instance.newSpeedValue = (double)numSpeed.Value;
        }

        private void comboSpeedUnits_SelectedValueChanged(object sender, EventArgs e)
        {
            BenbunPlugin.Instance.export_speedUnits = comboSpeedUnits.SelectedIndex;
            switch (comboSpeedUnits.SelectedIndex)
            {
                case 2:
                    BenbunPlugin.Instance.newSpeedUnits = "minutespermile";
                    break;
                case 3:
                    BenbunPlugin.Instance.newSpeedUnits = "minutesperkm";
                    break;
                case 1:
                    BenbunPlugin.Instance.newSpeedUnits = "kmperhour";
                    break;
                case 0:
                    BenbunPlugin.Instance.newSpeedUnits = "milesperhour";
                    break;
                default:
                    break;
            }
        }

        private void rb1_CheckedChanged(object sender, EventArgs e)
        {
            if (rb1.Checked)
            {
                //label2.Enabled = false;
                numSpeed.Enabled = false;
                comboSpeedUnits.Enabled = false;
            }
            else
            {
                //label2.Enabled = true;
                numSpeed.Enabled = true;
                comboSpeedUnits.Enabled = true;
            }

        }
    }
}